package com.yanqu.road.server.manger.player;

import com.yanqu.road.entity.event.EventTriggerArgs;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.player.impl.PlayerOnlineTimeChangeForAd;
import com.yanqu.road.server.manger.player.task.PlayInfoChangeType;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class PlayerInfoChangeNotifyMgr extends TempMgr {

    private static Map<Integer, List<IPlayInfoChangeListener>> listMap = new ConcurrentHashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        addSyncListener(PlayInfoChangeType.DailyOnlineTime.getType(), new PlayerOnlineTimeChangeForAd());
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void addSyncListener(int type, IPlayInfoChangeListener syncListener){
        listMap.putIfAbsent(type, new ArrayList<>());
        listMap.get(type).add(syncListener);
    }

    public static void notifyReload(int type, long userId, EventTriggerArgs args){
        List<IPlayInfoChangeListener> listeners = listMap.get(type);
        if(listeners == null){
            return;
        }
        for (IPlayInfoChangeListener listener : listeners) {
            try {
                listener.notify(type, userId, args);
            }catch (Exception e) {
                getLogger().error("", e);
            }
        }
    }
}
